//package com.usian.admin.service.impl;
//
//import com.alibaba.fastjson.JSONArray;
//import com.usian.admin.feign.ArticleFeign;
//import com.usian.admin.feign.WemediaFeign;
//import com.usian.admin.mapper.AdChannelMapper;
//import com.usian.admin.mapper.AdSensitiveMapper;
//import com.usian.admin.service.WemediaNewsAutoScanService;
//import com.usian.common.aliyun.GreeTextScan;
//import com.usian.common.aliyun.GreenImageScan;
//import com.usian.common.fastdfs.FastDFSClient;
//import com.usian.model.admin.pojos.AdChannel;
//import com.usian.model.article.pojos.ApArticle;
//import com.usian.model.article.pojos.ApArticleConfig;
//import com.usian.model.article.pojos.ApArticleContent;
//import com.usian.model.article.pojos.ApAuthor;
//import com.usian.model.media.pojos.WmNews;
//import com.usian.model.media.pojos.WmUser;
//import com.usian.utils.common.SensitiveWordUtil;
//import io.seata.spring.annotation.GlobalTransactional;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//
//@Service
//@Log4j2
//public class WemediaNewsAutoScanServiceImpl implements WemediaNewsAutoScanService {
//    @Autowired
//    private WemediaFeign wemediaFeign;
//    @Autowired
//    private AdSensitiveMapper adSensitiveMapper;
//    @Autowired
//    private GreenImageScan greenImageScan;
//    @Autowired
//    private FastDFSClient fastDFSClient;
//    @Autowired
//    private GreeTextScan greeTextScan;
//    @Autowired
//    ArticleFeign articleFeign;
//    @Autowired
//    AdChannelMapper adChannelMapper;
//    @Value("${fdfs.url}")
//    private String fileServerUrl;
//    @GlobalTransactional
//    @Override
//    public void autoScanByMediaNewsId(Integer id) {
//        if (id==null){
//            log.error("当前的审核id空");
//            return;
//        }
//        //根据id查询文章信息
//        WmNews wmNews = wemediaFeign.findById(id);
//        if (wmNews==null){
//            log.error("审核文章不存在，id:{}",id);
//            return;
//        }
//        //人工审核通过（4）直接保存数据和创建索引
//        if (wmNews.getStatus()==4){
//            //保存数据
//            saveAppArticle(wmNews);
//            return;
//        }
//        //文章状态为8发布时间>大于当前时间直接保存数据
//        if (wmNews.getStatus()==8&&wmNews.getPublishTime().getTime()<=System.currentTimeMillis()){
//            //保存数据
//            saveAppArticle(wmNews);
//            return;
//        }
//        //状态为1待审核
//        if (wmNews.getStatus()==1){
//            //抽取文章内容中的存文本和图片
//            Map<String,Object> contentAndImagesResult= handleTextAndImages(wmNews);
//            //4.1文本审核
//            boolean textScanBoolean = handleTextScan((String)contentAndImagesResult.get("content"),wmNews);
//            if (!textScanBoolean)return;
//            //图片审核
//            boolean imagesScanBoolean = handleImagesScan((List<String>)contentAndImagesResult.get("images"),wmNews);
//            if (!imagesScanBoolean)return;
//            //敏感词
//            boolean sensitiveScanBoolean = handleSensitive((String)contentAndImagesResult.get("content"),wmNews);
//            if (!sensitiveScanBoolean)return;
//            //发布时间大于当前时间
//            if (wmNews.getPublishTime().getTime()>System.currentTimeMillis()){
//                //修改文章状态为8
//                updateWmNews(wmNews,(short)8,"审核通过待发布");
//                return;
//            }
//            //审核通过修改文章状态为9 保存app端相关文章信息
//            saveAppArticle(wmNews);
//        }
//    }
//    //修改文章
//    private void updateWmNews(WmNews wmNews, short i, String msg) {
//        wmNews.setStatus(i);
//        wmNews.setReason(msg);
//        wemediaFeign.updateWmNews(wmNews);
//    }
//    //敏感词审核
//    private boolean handleSensitive(String content, WmNews wmNews) {
//        boolean flag=true;
//        List<String> allSensitive = adSensitiveMapper.findAllSensitive();
//        //初始化敏感词
//        SensitiveWordUtil.initMap(allSensitive);
//        //文章内容自管理敏感词过滤
//        Map<String, Integer> integerMap = SensitiveWordUtil.matchWords(content);
//        if (integerMap.size()>0){
//            log.error("敏感词没通过 有:{}",integerMap);
//            //审核不通过
//            updateWmNews(wmNews,(short)2,"文章有敏感词");
//            flag=false;
//        }
//        return flag;
//    }
//    //图片审核
//    private boolean handleImagesScan(List<String> images, WmNews wmNews) {
//        if (images==null){
//            return true;
//        }
//        boolean flag=true;
//        ArrayList<byte[]> imagelist = new ArrayList<>();
//        try {
//            for (String image : images) {
//                String imageName = image.replace(fileServerUrl, "");
//                int index = imageName.indexOf("/");
//                String groupName = imageName.substring(0, index);
//                String imagePath = imageName.substring(index + 1);
//                byte[] imagebyte = fastDFSClient.download(groupName, imagePath);
//                imagelist.add(imagebyte);
//            }
//            //阿里云图片审核
//            Map map = greenImageScan.imageScan(imagelist);
//            //审核不通过
//            if (!map.get("suggestion").equals("pass")){
//                //审核失败
//                if (map.get("suggestion").equals("block")){
//                    //修改自媒体文章的状态并告知审核失败原因
//                    updateWmNews(wmNews,(short)2,"文章中图片有违规");
//                    flag=false;
//                }
//                //人工审核
//                if (map.get("sugestion").equals("review")) {
//                    //修改自媒体文章的状态并告知审核失败原因
//                    updateWmNews(wmNews, (short) 3, "文章图片有不确定元素");
//                    flag = false;
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            flag=false;
//        }
//        return flag;
//    }
//    //文本审核
//    private boolean handleTextScan(String content, WmNews wmNews) {
//        boolean flag=true;
//        try {
//            Map map = greeTextScan.greeTextScan(content);
//            //审核不通过
//            if (!map.get("suggestion").equals("pass")){
//                //审核失败
//                if (map.get("suggestion").equals("block")){
//                    //修改自媒体文章的状态，并告知审核失败原因
//                    updateWmNews(wmNews,(short) 2,"文章内容中有敏感词");
//                    flag=false;
//                }
//                //人工审核
//                if (map.get("suggestion").equals("review")){
//                    //修改自媒体文章的状态 并告知审核失败原因
//                    updateWmNews(wmNews,(short)3,"文章中有不确定敏感词");
//                    flag=false;
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            flag=false;
//        }
//        return flag;
//    }
//    //提取文本和图片
//    private Map<String, Object> handleTextAndImages(WmNews wmNews) {
//        //文章内容
//        String content = wmNews.getContent();
//        //存储文本内容
//        StringBuilder stringBuilder = new StringBuilder();
//        //存储图片
//        List<String> image = new ArrayList<>();
//        List<Map> list = JSONArray.parseArray(content, Map.class);
//        for (Map map : list) {
//            if (map.get("type").equals("text")){
//                stringBuilder.append(map.get("value"));
//            }
//            if (map.get("type").equals("image")){
//                image.add((String) map.get("value"));
//            }
//        }
//        if (wmNews.getImages() !=null && wmNews.getType()!=0){
//            String[] split = wmNews.getImages().split(",");
//            image.addAll(Arrays.asList(split));
//        }
//        Map<String, Object> hashMap = new HashMap<>();
//        hashMap.put("content",stringBuilder.toString());
//        hashMap.put("images",image);
//        return hashMap;
//    }
//    //保存App文章相关数据
//    private void saveAppArticle(WmNews wmNews) {
//        //保存app文章
//        ApArticle apArticle=saveArticle(wmNews);
//        //保存app文章配置
//        saveArticleConfig(apArticle);
//        //保存App文章内容
//        saveArticleContent(apArticle,wmNews);
//        //修改文章的状态为9
//        updateWmNews(wmNews,(short)9,"审核通过");
//        //TODO es索引创建
//    }
//    //保存文章
////    private ApArticle saveArticle(WmNews wmNews) {
////        ApArticle apArticle = new ApArticle();
////        apArticle.setTitle(wmNews.getTitle());
////        apArticle.setLayout(wmNews.getType());
////        apArticle.setImages(wmNews.getImages());
////        apArticle.setCreatedTime(new Date());
////        //获取作者相关信息
////        Integer userId = wmNews.getUserId();
////        WmUser wmUserById = wemediaFeign.findWmUserById(userId);
////        if (wmUserById!=null){
////            String name = wmUserById.getName();
////            ApAuthor apAuthor = articleFeign.selectAuthorByName(name);
////            if (apAuthor!=null){
////                apArticle.setAuthorId(apAuthor.getId().longValue());
////                apArticle.setAuthorName(apAuthor.getName());
////            }
////        }
////        //获取频道相关信息
////        Integer channelId = wmNews.getChannelId();
////        AdChannel adChannel = adChannelMapper.selectById(channelId);
////        if (adChannel!=null){
////            apArticle.setChannelId(adChannel.getId());
////            apArticle.setChannelName(adChannel.getName());
////        }
////        return articleFeign.saveArticle(apArticle);
////    }
//    private ApArticle saveArticle(WmNews wmNews) {
//        ApArticle apArticle = new ApArticle();
//        apArticle.setTitle(wmNews.getTitle());
//        apArticle.setLayout(wmNews.getType());
//        apArticle.setImages(wmNews.getImages());
//        apArticle.setCreatedTime(new Date());
//
//        //获取作者相关信息
//        Integer wmUserId = wmNews.getUserId();
//        WmUser wmUser = wemediaFeign.findWmUserById(wmUserId);
//        if(wmUser != null){
//            String wmUserName = wmUser.getName();
//            ApAuthor apAuthor = articleFeign.selectAuthorByName(wmUserName);
//            if(apAuthor != null){
//                apArticle.setAuthorId(apAuthor.getId().longValue());
//                apArticle.setAuthorName(apAuthor.getName());
//            }
//
//        }
//
//        //获取频道相关信息
//        Integer channelId = wmNews.getChannelId();
//        AdChannel channel = adChannelMapper.selectById(channelId);
//        if(channel != null){
//            apArticle.setChannelId(channel.getId());
//            apArticle.setChannelName(channel.getName());
//        }
//
//        return articleFeign.saveArticle(apArticle);
//    }
//    //创建APP端文章内容信息
//    private void saveArticleContent(ApArticle apArticle, WmNews wmNews) {
//        ApArticleContent apArticleContent = new ApArticleContent();
//        apArticleContent.setArticleId(apArticle.getId());
//        apArticleContent.setContent(wmNews.getContent());
//        articleFeign.saveArticleContent(apArticleContent);
//
//    }
//    //创建APP端文章配置信息
//    private void saveArticleConfig(ApArticle apArticle) {
//        ApArticleConfig apArticleConfig = new ApArticleConfig();
//        apArticleConfig.setArticleId(apArticle.getId());
//        apArticleConfig.setIsForward(true);
//        apArticleConfig.setIsDelete(false);
//        apArticleConfig.setIsDown(true);
//        apArticleConfig.setIsComment(true);
//        articleFeign.saveArticleConfig(apArticleConfig);
//    }
//
//}
